#-*-encoding:utf-8-*-

def write_vtypes(fname, vtypes):
    with open(fname, "wb") as f:
        f.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
        f.write("<routes>\n")
        attrib = ["id", "accel", "decel", "sigma", "length", "minGap", "maxSpeed", "speedFactor", "speedDev", "tau"]
        for vtype in vtypes:
            line = '\t<vType vClass="passenger"'
            for key in attrib:
                line += (' {0}="{1}"'.format(key, vtype[key]))
            line += " />\n"
            f.write(line)
        f.write("</routes>\n")

def write_routes(fname, trips):
    # with open(fname, "wb") as f:
    #     f.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
    #     f.write("<routes>\n")
    #     attrib = ["id", "arrivalPos", "arrivalSpeed", "depart", "departLane", "departPos", "departSpeed", "type"]
    #     for route in routes:
    #         line = "\t<vehicle "
    #         for key in attrib:
    #             if key in route:
    #                 line += (' {0}="{1}"'.format(key, route[key]))
    #         line += " >\n"
    #         f.write(line)
    #         f.write('\t\t<route edges="{0}" />\n'.format(route['route']))
    #         f.write('\t</vehicle>\n')
    #     f.write("</routes>\n")
    attribs = ['arrivalPos', 'arrivalSpeed', 'depart', 'departLane', 'departPos', 'departSpeed', 'id', 'type']
    with open(fname, "wb") as f:
        f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
        f.write('<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">\n')
        for trip in trips:
            line = '\t<vehicle'
            for h in attribs:
                if h in trip and trip[h]:
                    line += ' {0}="{1}"'.format(h, trip[h])
            line +=' >\n'
            f.write(line)
            if 'route' in trip:
                if isinstance(trip['route'], list):
                    f.write('\t\t<route edges="{0}" />\n'.format(" ".join(trip['route'])))
                else:
                    f.write('\t\t<route edges="{0}" />\n'.format(trip['route']))
            if 'stop' in trip:
                assert type(trip['stop']) is dict
                # f.write('\t\t<stop lane="{0}" endPos="{1}" duration="{2}"')
                stop_string = '\t\t<stop'
                for key in trip['stop']:
                    stop_string += ' {0}="{1}"'.format(key, trip['stop'][key])
                stop_string += " />\n"
                f.write(stop_string)
            f.write('\t</vehicle>\n')
        f.write('</routes>\n')

def write_sumo_cfg(fname, sumo_config):
    with open(fname, "wb") as f:
        f.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
        f.write("<configuration>\n")
        for node in sumo_config:
            f.write("\t<{0}>\n".format(node))
            for key, value in sumo_config[node].iteritems():
                f.write('\t\t<{0} value="{1}"/>\n'.format(key, value))
            f.write('\t</{0}>\n'.format(node))
        f.write("</configuration>\n")